<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
	body,html{
		margin: 0 auto;
		height:100%;
		width:100%;
	}
</style>
</head>
<body>
<div class="divfrist" id="power_mainDiv" style="width:100%;height:100%;overflow: hidden;">
		<div style="width:100%;height:1%;"></div>
		<div style="width:100%;height:97.9%">
				<div style="width:0.5%;height:100%;float: left;"></div>
				<div style="width:99%;height:100%;float: left;">
					 <div class="allborder" style="height:100%;max-height:100%;border: 0px solid #9ABAE9;overflow: hidden;">
								<!-- 内容区 -->
										  <div style="height:100%;width:100%;">
												<table  id="power_table" style="border: 0px; width: 100%"></table>
										  </div>
											<div id="power_tb" style="overflow: hidden; padding-left: 10px; padding-top: 5px;padding-bottom: 5px;">
																	 	<a href="javascript:void(0);" class="easyui-linkbutton"  data-options="iconCls:'icon58',plain:true" onclick="window.power.disEndPower(0)">禁用权限</a>||																		     												
																		<a href="javascript:void(0);" class="easyui-linkbutton"  data-options="iconCls:'icon59',plain:true" onclick="window.power.disEndPower(1)">启用权限</a>||
											</div>
								<!-- 内容区 -->
					</div>	
				</div>
				<div style="width:0.5%;height:100%;float: left;"></div>
		</div>
		<div style="width:100%;height:1%"></div>
</div>
</body>
<script type="text/javascript">
(function(){
	$(function(){	
		$.parser.parse('#power_mainDiv'); 
		 createTreeGrid();
	});
	//创建权限树并初始数据
	function createTreeGrid(){
		$('#power_table').treegrid({
			fit: true,
			//title:'权限列表',
			rownumbers: true,
			animate:true,
			loadMsg:'数据正在加载...',
			//collapsible:true,
			fitColumns:true,
			striped: true,
			idField:'id',
			toolbar:"#power_tb",
			treeField:'text',
			onExpand:function(row){ 
				var flag = $("#ckid"+row.id)[0].checked;
				if(flag==true){
						var temp =$("[parent_id ="+row.id+"]");
						for ( var i = 0; i < temp.length; i++) {	  	 
					  	     $(temp[i])[0].checked=true;
					 	}
				} 
			},
			columns:[[
				{field:'id',title:'',
						 formatter : function(value, row, index){
									return  '<input type="checkbox" id="ckid'+row.id+'" name="treeCK"   value="ckid'+row.parent_id+'" parent_id="'+row.parent_id+'" uuid="'+row.sysPower_id+'" treeid="'+row.id+'"/>';
					},width:30},	
		  		{field:'text',title:'权限名称',width:120},
		  		{field:'sysPower_id',title:'权限ID',width:120,hidden:true},
				//{field:'sysPower_name',title:'<fmt:message key="com.web.sys.resour.sysPower_name"></fmt:message>',width:120},
				//{field:'sysPower_treeid',title:'<fmt:message key="com.web.sys.resour.sysPower_treeid"></fmt:message>',width:120},
				{field:'sysPower_state',title:'权限状态',formatter : function(value, row, index){
					
					if(row.sysPower_state=="0"){
						return  '<font color="red">禁用</font>';
					}
					if(row.sysPower_state=="1"){
						return  '<font color="green">启用</font>';
					}
					return "";
			},width:120},
				//{field:'sysPower_parent_id',title:'<fmt:message key="com.web.sys.resour.sysPower_parent_id"></fmt:message>',width:120},
				{field:'sysPower_icon',title:'图标',
					 formatter : function(value, row, index){
						return  "<div style='width:16px;height:16px;border:0px red solid;' class='"+row.sysPower_icon+"'></div>";
					},width:50},
				{field:'sysPower_url',title:'路径',
						formatter : function(value, row, index){			
						if(row.sysPower_type=="1"){
							return  row.sysPower_url;
						}
						return "";
					},width:120},
				{field:'sysPower_type',title:'类型',
						formatter : function(value, row, index){
					
							if(row.sysPower_type=="0"){
								return  '<font color="red">系统权限</font>';
							}
							if(row.sysPower_type=="1"){
								return  '<font color="black">外部权限</font>';
							}
							if(row.sysPower_type=="2"){
								return  '<font color="green">菜单权限</font>';
							}
							return "";
					},width:120},
				{field:'sysPower_note',title:'权限描述',width:120},
				{field:'sysPower_CreateDate',title:'权限创建时间',width:120}
				]],
				onClickRow:function(row){
                       if($("#ckid"+row.id)[0].checked==false){
                    	     $("#ckid"+row.id)[0].checked=true;
                         }else{
                        	 $("#ckid"+row.id)[0].checked=false;
                        }				
					TreeCK($("#ckid"+row.id)[0]);								
				},
				pagination:false
				
			});
		  
			var p = $('#power_table').treegrid('getPager');
			$(p).pagination({
						pageSize : 15, 
						pageList :[15,20,25],
						beforePageText : '第',
						afterPageText : '页    共 {pages} 页',
						displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
						onSelectPage : function(pageNumber, pageSize){
								PageIndex = pageNumber;
								PageSize = pageSize;
								var start = (pageNumber - 1) * pageSize;
								var urldata = "start=" + start + "&";
									urldata += "limit=" + pageSize;
									urldata = encodeURI(urldata);
									$.ajax({
										type : "POST",
										dataType:'text',
										url : '<%=request.getContextPath()%>/sys/sysPowerAction!getAllPowerByTreeID.action',
										data : urldata,
										success : function(msg){
											var temp = eval('(' + msg + ')');
											$('#power_table').treegrid("loadData", temp);
										}
									});
						}
			});
			//加载数据
			selectAllPower();
	}
	//增加权限和更新权限的入口
	function addPower(){
		$('#power_table').treegrid("unselectAll");
		 addPowerdiag();
	}
	//增加权限
	function addPowerdiag(){
		$('<div></div>').window({
			id : "addPower",
			title:'增加权限',
			width:630,
			modal: true,
			height:390,
			content:'<iframe frameborder="0"  scrolling="no" width="100%" height="100%" src="${pageContext.request.contextPath}/jsp/system/addPower.jsp?r='+new Date()+'"></iframe>'
		});
	}
	//更新
	function updatePower(){
		var b =choosePowerisPass();
		if(b==true){
			return;
		}
		addPowerdiag();
	}
	//删除
	function deletePower(){
		$.messager.confirm('提示','你确定要删除数据吗？', function(r){		
			if(r){
				var sysPowerArray = new Array();
				for(var i=0;i<$("[name=treeCK]").length;i++){
					if($("[name=treeCK]")[i].checked==true){
						 var sysPower = new Object();
						 sysPower.sysPower_id=$($("[name=treeCK]")[i]).attr("uuid");
						 sysPowerArray.push(sysPower);
					}
				}
				if(sysPowerArray.length<=0){
					alert("未选中数据");
					return;
				}
				var allPower = obj2str(sysPowerArray);
				$.ajax({
					   type: "POST",
					   dataType:'text',
					   url: "<%=request.getContextPath()%>/sys/sysPowerAction!deletePower.action",
					   processData: true,
					   data:{allPower:allPower},
					   dataType:'text',
					   success: function(msg){
							if(msg=="sucess"){
								 alert("删除成功");
								$('#power_table').treegrid("reload");
							}else if(msg=="isSys"){
								 alert("系统权限不能删除");
							}else{
								 alert("删除成功");
							}
					   },
					  error:function (XMLHttpRequest, textStatus, errorThrown) {
						  alert("访问错误");
					  }
					});
			}
		});
	}
	//禁用启用
	function disEndPower(sysPowerstate){
		var sysPowerArray = new Array();
		for(var i=0;i<$("[name=treeCK]").length;i++){
			if($("[name=treeCK]")[i].checked==true){
				 var sysPower = new Object();
				 sysPower.sysPower_id=$($("[name=treeCK]")[i]).attr("uuid");
				 sysPowerArray.push(sysPower);
			}
		}
		if(sysPowerArray.length<=0){
			alert("未选中数据");
			return;
		}
		var allPower = obj2str(sysPowerArray);
		$.ajax({
			   type: "POST",
			   dataType:'text',
			   url: "<%=request.getContextPath()%>/sys/sysPowerAction!updateTMore.action",
			   processData: true,
			   data:{allPower:allPower,sysPower_state:sysPowerstate},
			   dataType:'text',
			   success: function(msg){
					if(msg=="sucess"){
						alert("操作成功");
						$('#power_table').treegrid("reload");
					}else{
						alert("操作失败");
					}
			   },
			  error:function (XMLHttpRequest, textStatus, errorThrown) {
					alert("访问错误");
			  }
			});
	}
	function choosePowerisPass(){
		var sysPower1=$('#power_table').treegrid("getSelected");
		if(sysPower1==null){		
			 alert("数据未选中");
			return true;
		}
		if($("#ckid"+sysPower1.id)[0].checked==false){
			 alert("数据未选中");
			return true;
		}
		if(sysPower1.sysPower_type=="0"||sysPower1.sysPower_type=="2"){
			 alert("系统权限");
			return true;
		}	
	}
	//查询全部权限
	function selectAllPower(){
		$.ajax({
			   type: "POST",
			   dataType:'text',
			   url: "<%=request.getContextPath()%>/sys/sysPowerAction!getAllPowerByTreeID_ID0.action",
			   success: function(msg){
				  	var temp= eval('('+msg+')');
				   	$("#power_table").treegrid("loadData",temp);
				 	$("#power_table").treegrid("options").url="<%=request.getContextPath()%>/sys/sysPowerAction!getAllPowerByTreeID.action";	
			   },
			  error:function (XMLHttpRequest, textStatus, errorThrown) {
				   showError1();
			  }
			});
	}
	function TreeCK(treeNode){
		  var flag = $(treeNode)[0].checked;
		  var temp =treeNode;
		  if(flag==true){
		  		//向上全部选中
		 			// forCKUP(temp,"true");  	
		 			 forCKDown(temp,"true");
		}
		if(flag==false){
				 forCKDown(temp,false);
		}
	}
	//选择父权限，子权限选中
	function forCKUP(ck,flag){
		  var parent_id = $(ck).attr("parent_id");
		  var temp1 =$("[treeid ="+parent_id+"]");
		  if(temp1.length ==0){
		  	return;
		  }
			var temp =$("[treeid ="+parent_id+"]")[0];
		temp.checked=true;	
		forCKUP(temp,flag);
	}
	function forCKDown(ck,flag){
			 var treeid = $(ck).attr("treeid");
			 var temp =$("[parent_id ="+treeid+"]");
	   	for ( var i = 0; i < temp.length; i++) {	
	   				$(temp[i])[0].checked =flag;
	   			 		forCKDown($(temp[i])[0],flag);
	   	}   
	}
	//注册所有事件
		window["power"]={};
		//window["power"]["addPower"]=addPower;
		//window["power"]["updatePower"]=updatePower;
	 	//window["power"]["deletePower"]=deletePower;
		window["power"]["disEndPower"]=disEndPower;
	
	
})();
</script>

</html>